using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using MySQLStudio.DataAccess;
using MySQLStudio.BusinessLogic;

namespace MySQLStudio.GUI.Controls
{
    public partial class ConnectToHost : Telerik.WinControls.UI.RadForm
    {       
        enum eLocalHost
        {
            _HOST_NAME,
            _PORT_NUMBER
        };

        public delegate void EnableConnectButtonDelegate(RadButton btnConnect);
        public delegate void CloseConnectDialogDelegate();

        private int defaultPortNumber = 3306;

        private Host m_host;

        public string HostName
        {
            get { return m_host.Hostname; }
            set { m_host.Hostname = value; }
        }
        
        public Host Host
        {
            get { return m_host; }  
        }

        public string Username
        {
            get { return m_host.Username; }
            set { m_host.Username = value; }
        }

        public string Password
        {
            get { return m_host.Password; }
            set { m_host.Password = value; }
        }
        public int PortNumber
        {
            get { return m_host.PortNumber; }
            set { m_host.PortNumber = value; }
        }


        public ConnectToHost()
        {
            InitializeComponent();

            this.m_host = new Host();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            m_host = null;
            this.Close();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            btnConnect.Enabled = false;

            String[] tmpHostNameAndPort = this.txtHostName.Text.Split(':') ;

            if (tmpHostNameAndPort.Length > 0)
            {
                this.HostName = tmpHostNameAndPort[(int)eLocalHost._HOST_NAME];
                // if localHost test have this format : localhost:80
                if (tmpHostNameAndPort.Length > 1)
                    this.PortNumber = int.Parse(tmpHostNameAndPort[(int)eLocalHost._PORT_NUMBER]);
                else
                    this.PortNumber = defaultPortNumber;

                this.Username = this.txtUserName.Text;
                this.Password = this.txtPassword.Text;

                ThreadPool.QueueUserWorkItem(new WaitCallback(EstablishConnectivity), null);               
            }
            else
                this.btnCancel_Click(sender, e);
        }
       

        private void EstablishConnectivity(object connectivityState)
        {
            bool connectionAvailable =  HostController.TryConnectToHost(this.m_host.Hostname,
                                                                        this.m_host.PortNumber,
                                                                        this.m_host.Username,
                                                                        this.m_host.Password);

            if (btnConnect.InvokeRequired)
                btnConnect.Invoke(new EnableConnectButtonDelegate(this.EnableConnectButton),
                                                                  new object[] {btnConnect });
            else
                btnConnect.Enabled = true;
            

            if (!connectionAvailable)
                MessageBox.Show(string.Format(ErrorMessages.ConnectionToHostFailed, this.m_host.Hostname));
            else
            {
                if (this.InvokeRequired)
                    this.Invoke(new CloseConnectDialogDelegate(CloseDialog),
                                    null);
                else
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }            
        }

        private void CloseDialog()
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void EnableConnectButton(RadButton btnConnect)
        {
            btnConnect.Enabled = true;
        }
    }
}
